package nsoft.timeline.util;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nsoft.timeline.object.MArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

@SuppressWarnings("unchecked")
public class session {
	public static String SERVER_SPLITTER = "<nsoft_server_splitter>";
	public static String SPLITTER = "<nsoft_splitter>";
	
	public static String SLIDE_LIST = "slide_list";
	public static String SLIDES = "slides";
	public static String IMAGE_SLIDES = "imgslides";
	public static String U2B_SLIDES = "u2bslides";
	public static String ARTICLE_SLIDES = "articleslides";
	public static String FB_SLIDES = "fbslides";
	public static String AUDIO_SLIDE = "audioslide";
	public static String TIMELINE_ORDERED_LIST = "tl_ordered_list";
	
	public static String TYPE_ADD = "add";
	public static String TYPE_REMOVE = "remove";
	public static String TYPE_REPLACE = "replace";
	public static String TYPE_UPDATE = "update";
	public static String TYPE_UPDATE_POSITION = "update-pos";
	public static String TYPE_MOVE = "move";
	public static String TYPE_MOVEBACK = "moveback";
	public static String TYPE_RETRIEVE = "retrieve";
	public static String TYPE_CLEAR = "clear";
	public static String TYPE_RETRIEVE_SLIDES = "retrieve-slides";
	
	static String[] slideTypes = { SLIDES, IMAGE_SLIDES, 
			U2B_SLIDES, ARTICLE_SLIDES, FB_SLIDES, AUDIO_SLIDE };
	
	static String AUDIO_DEFAULT_ID = "audio_id_@3883";
	
	/*
	public static void contactSession(HttpServletRequest req, 
										HttpServletResponse resp){
		String type = (String)req.getParameter("type");
		String listType = (String)req.getParameter("list");
		String id = (String)req.getParameter("id");
		String content = (String)req.getParameter("content");
		
		PrintWriter out = null;
		try{
			out = resp.getWriter();
		}catch(IOException ioEx){
			System.err.println(ioEx.getClass() + ": " + ioEx.getMessage());
		}
		if (out == null) {
			System.err.println("server is not responsible.");
			return;
		}
		
		HashMap<String, HashMap<String, String>> slides = null;
		HttpSession currSession = req.getSession();
		
		if (currSession.getAttribute(SLIDE_LIST) != null){
			slides = (HashMap<String, HashMap<String, String>>)
					currSession.getAttribute(SLIDE_LIST);
		}else{
			slides = new HashMap<String, HashMap<String, String>>();
			for (int i = 0; i < slideTypes.length; i++){
				slides.put(slideTypes[i], new HashMap<String, String>());
			}
			currSession.setAttribute(SLIDE_LIST, slides);
		}
		
		
		if (type == null) return;
		
		if (!type.contains(TYPE_RETRIEVE)){
			HashMap<String, String> list = slides.get(listType);
			if (type.equals(TYPE_ADD)){
				list.put(id, content);
			}else if (type.equals(TYPE_REMOVE)){
				list.remove(id);
			}else if (type.equals(TYPE_MOVE)){
				String tempContent = list.get(id);
				slides.get(SLIDES).put(id, tempContent);
				list.remove(id);
			}else if (type.equals(TYPE_MOVEBACK)){
				String tempContent = slides.get(SLIDES).get(id);
				list.put(id, tempContent);
				slides.get(SLIDES).remove(id);
			}else if (type.equals(TYPE_UPDATE)){
				list.remove(id);
				list.put(id, content);
			}else if (type.equals(TYPE_CLEAR)){
				list.clear();
			}
		}else if (type.equals(TYPE_RETRIEVE)){
			JSONObject json = new JSONObject(slides);
			out.print(json.toString());
			out.flush();
		}else if (type.equals(TYPE_RETRIEVE_SLIDES)){
			JSONObject json = new JSONObject(slides.get(SLIDES));
			out.print(json.toString());
			out.flush();
		}

	}
	*/
	
	public static void contactSession(HttpServletRequest req, 
									HttpServletResponse resp){
		String type = (String)req.getParameter("type");
		String listType = (String)req.getParameter("list");
		String id = (String)req.getParameter("id");
		String content = (String)req.getParameter("content");

		PrintWriter out = null;
		try{
			resp.setContentType("text/html; charset=UTF-8");
			out = resp.getWriter();
		}catch(IOException ioEx){
			System.err.println(ioEx.getClass() + ": " + ioEx.getMessage());
		}
		if (out == null) {
			System.err.println("server is not responsible.");
			return;
		}
		
		HashMap<String, MArrayList> slides = null;
		HttpSession currSession = req.getSession();
		
		if (currSession.getAttribute(SLIDE_LIST) != null){
			slides = (HashMap<String, MArrayList>)
					currSession.getAttribute(SLIDE_LIST);
		}else{
			slides = new HashMap<String, MArrayList>();
			for (int i = 0; i < slideTypes.length; i++){
				slides.put(slideTypes[i], new MArrayList());
			}
			currSession.setAttribute(SLIDE_LIST, slides);
		}

		/** if request is not concerned */
		if (type == null) return;
		
		if (!type.contains(TYPE_RETRIEVE)){
			MArrayList list = slides.get(listType);
			if (type.equals(TYPE_ADD)){
				list.add(id + SERVER_SPLITTER + content);
			}else if (type.equals(TYPE_REMOVE)){
				String prevData=list.getContentById(id);
				deleteFile(prevData);
				list.removeById(id);
			}else if (type.equals(TYPE_REPLACE)){
				String prevData=list.
						getContentById(AUDIO_DEFAULT_ID);
				deleteFile(prevData);
				list.removeById(AUDIO_DEFAULT_ID);
				
				list.add(AUDIO_DEFAULT_ID+
						SERVER_SPLITTER+content);
			}else if (type.equals(TYPE_MOVE)){
				String tempContent = list.getContentById(id);
				slides.get(SLIDES).add(tempContent);
				list.removeById(id);
			}else if (type.equals(TYPE_MOVEBACK)){
				String tempContent = slides.
							get(SLIDES).getContentById(id);
				list.add(tempContent);
				slides.get(SLIDES).removeById(id);
			}else if (type.equals(TYPE_UPDATE)){
				String tempContent = id + SERVER_SPLITTER + content;
				list.updateContentById(id, tempContent);
			}else if (type.equals(TYPE_CLEAR)){
				deleteAll(list);
				list.clear();
			}else if (type.equals(TYPE_UPDATE_POSITION)){
				String tempContent = list.getContentById(id);
				list.removeById(id);
				int new_index = Integer.parseInt(content);
				list.add(new_index, tempContent);
			}
		}else if (type.equals(TYPE_RETRIEVE)){
			JSONObject json = new JSONObject(slides);
			out.print(json.toString());
			out.flush();
		}else if (type.equals(TYPE_RETRIEVE_SLIDES)){
			JSONArray json = new JSONArray(slides.get(SLIDES));
			if (slides.get(AUDIO_SLIDE).size() > 0)
				json.put(slides.get(AUDIO_SLIDE).get(0));
			out.print(json.toString());
			out.flush();
		}
	}

	private static boolean deleteAll(MArrayList list){
		boolean ret=true;
		String content=null;
		for (int i =0; i<list.size();i++){
			content=list.get(i);
			ret=ret & deleteFile(content);
		}
		return ret;
	}
	
	private static boolean deleteFile(String content){
		boolean ret=true;
		
		if (content==null || content.equals("")){
			return false;
		}
		
		String localPath=System.getProperty("wtp.deploy") + 
							"/timeline";
		
		String[] data=content.split(SPLITTER);
		File fThumb=new File(localPath+data[1].substring(1));
		if (fThumb.exists()){
			ret=ret & fThumb.delete();
		}
		File fData=new File(localPath+data[2].substring(1));
		if (fData.exists()){
			ret=ret & fData.delete();
		}
		return ret;
	}

}
